﻿using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using OrBitDataModule.UI.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrBitDataModule.Models;
using System.IO.Ports;

namespace OrBitDataModule.UI.Setting
{
    public partial class SerialPortSetting : SettingForm
    {
        public SerialPortSetting()
        {
            InitializeComponent();
            this.Load += SerialPortSetting_Load;
        }

        private void SerialPortSetting_Load(object sender, EventArgs e)
        {
            for (int i = 1; i < 50; i++)
            {
                cb_port.Items.Add("COM" + i.ToString());
            }
            cb_DataBit.Items.Add(5);
            cb_DataBit.Items.Add(6);
            cb_DataBit.Items.Add(7);
            cb_DataBit.Items.Add(8);
            cb_DataBit.SelectedItem = 8;

            cb_encoding.Items.AddRange(Encoding.GetEncodings().Select(c => c.Name).ToArray());
            cb_encoding.Text = "utf-8";

            //奇偶较验位
            cb_Parity.Items.Add(System.IO.Ports.Parity.Even);
            cb_Parity.Items.Add(System.IO.Ports.Parity.Mark);
            cb_Parity.Items.Add(System.IO.Ports.Parity.None);
            cb_Parity.Items.Add(System.IO.Ports.Parity.Odd);
            cb_Parity.Items.Add(System.IO.Ports.Parity.Space);
            cb_Parity.SelectedItem = System.IO.Ports.Parity.None;

            //停止位
            cb_StopBit.Items.Add(System.IO.Ports.StopBits.None);
            cb_StopBit.Items.Add(System.IO.Ports.StopBits.One);
            cb_StopBit.Items.Add(System.IO.Ports.StopBits.OnePointFive);
            cb_StopBit.Items.Add(System.IO.Ports.StopBits.Two);
            cb_StopBit.SelectedItem = System.IO.Ports.StopBits.One;
        }

        public override string SettingName => "串口通讯";
        public override string SettingType => "SerialPort";
        public override string GetSetting()
        {
            if (string.IsNullOrWhiteSpace(this.cb_encoding.Text)) throw new Exception("编码不能为空");
            if (string.IsNullOrWhiteSpace(this.cb_port.Text)) throw new Exception("串口号不能为空");
            if (string.IsNullOrWhiteSpace(this.cb_BaudRate.Text)) throw new Exception("波特率不能为空");

            SerialPortSettingModel settingModel = new SerialPortSettingModel
            {
                PortName = cb_port.Text,
                BaudRate = Convert.ToInt32(cb_BaudRate.Text),
                Parity = (Parity)cb_Parity.SelectedItem,
                DataBits = (int)cb_DataBit.SelectedItem,
                StopBits = (System.IO.Ports.StopBits)cb_StopBit.SelectedItem,
                DelayMilliseconds = Convert.ToInt32(numReceiveTimeOut.Value),
                DefaultEncoding = cb_encoding.Text
            };
            return JsonConvert.SerializeObject(settingModel);
        }
        public override void LoadSetting(string json)
        {
            SerialPortSettingModel settingModel = JsonConvert.DeserializeObject<SerialPortSettingModel>(json);

            cb_port.Text = settingModel.PortName;
            cb_BaudRate.Text = settingModel.BaudRate.ToString();
            cb_Parity.SelectedItem = settingModel.Parity;
            cb_DataBit.SelectedItem = settingModel.DataBits;
            cb_StopBit.SelectedItem = settingModel.StopBits;
            numReceiveTimeOut.Value = settingModel.DelayMilliseconds;
            cb_encoding.Text = settingModel.DefaultEncoding;
        }
    }
}
